<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类别" prop="category">
        <el-select v-model="queryParams.category" placeholder="请选择会议室类别">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="roomName">
        <el-input
          v-model="queryParams.roomName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!--<el-form-item label="楼层" prop="floor">-->
        <!--<el-input-->
          <!--v-model="queryParams.floor"-->
          <!--placeholder="请输入楼层"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="房号" prop="houseNumber">
        <el-input
          v-model="queryParams.houseNumber"
          placeholder="请输入房号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['park:buiMeeting:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['park:buiMeeting:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['park:buiMeeting:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['park:buiMeeting:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="buiMeetingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会议室类别" align="center" prop="category" />
      <el-table-column label="名称" align="center" prop="roomName" />
      <el-table-column label="租赁方式" align="center" prop="leaseWay" />
      <!--<el-table-column label="楼栋id" align="center" prop="buildingId" />-->
      <!--<el-table-column label="楼层" align="center" prop="floor" />-->
      <el-table-column label="容纳人数" align="center" prop="capacity" />
      <el-table-column label="面积(m²)" align="center" prop="builtUpArea" />
      <!--<el-table-column label="状态" align="center" prop="state" />-->
      <el-table-column label="出租价格" align="center" prop="areaPrice" />
      <el-table-column label="特点" align="center" prop="feature" />
      <el-table-column label="房号" align="center" prop="houseNumber" />
      <!--<el-table-column label="费用" align="center" prop="fee" />-->
      <!--<el-table-column label="地址" align="center" prop="address" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="showFileDowFn(scope.row)"
            v-hasPermi="['park:buiMeeting:edit']"
          >下载</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['park:buiMeeting:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['park:buiMeeting:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <FileDownPop v-if="showFilePop" @close="filePopClose" :fileList="fileList"></FileDownPop>

    <!-- 添加或修改会务管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!--<el-form-item label="编号" prop="id">-->
          <!--<el-input v-model="form.id" placeholder="请输入编号" />-->
        <!--</el-form-item>-->
        <el-form-item label="类别" prop="category">
          <el-select v-model="form.category" placeholder="请选择类别">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="roomName">
          <el-input v-model="form.roomName" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="租赁方式" prop="leaseWay">
          <el-select v-model="form.leaseWay" placeholder="请选择租赁方式">
            <el-option
              v-for="item in lease_ways"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="楼栋id" prop="buildingId">-->
          <!--<el-input v-model="form.buildingId" placeholder="请输入楼栋id" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="楼层" prop="floor">-->
          <!--<el-input v-model="form.floor" placeholder="请输入楼层" />-->
        <!--</el-form-item>-->
        <el-form-item label="容纳人数(人)" prop="capacity">
          <el-input v-model="form.capacity" placeholder="请输入容纳人数" />
        </el-form-item>
        <el-form-item label="面积(㎡)" prop="builtUpArea">
          <el-input v-model="form.builtUpArea" placeholder="请输入面积" />
        </el-form-item>
        <el-form-item label="附件">
          <FileUpload  ref="fileupload"  :fileListTem="form.fileList" :fileType="['png','jpg','jpeg']" />
        </el-form-item>
        <!--<el-form-item label="状态" prop="state">-->
          <!--<el-input v-model="form.state" placeholder="请输入状态" />-->
        <!--</el-form-item>-->
        <el-form-item label="出租价格" prop="areaPrice">
          <el-input v-model="form.areaPrice" placeholder="请输入出租价格" />
        </el-form-item>
        <el-form-item label="特点" prop="feature">
          <el-input v-model="form.feature" placeholder="请输入特点" />
        </el-form-item>
        <!--<el-form-item label="费用" prop="fee">-->
          <!--<el-input v-model="form.fee" placeholder="请输入费用" />-->
        <!--</el-form-item>-->
        <el-form-item label="房号" prop="houseNumber">
          <el-input v-model="form.houseNumber" placeholder="请输入房号" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>

        <el-divider content-position="center">会议室配套信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddBuiMeetingFacility">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBuiMeetingFacility">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="buiMeetingFacilityList" :row-class-name="rowBuiMeetingFacilityIndex" @selection-change="handleBuiMeetingFacilitySelectionChange" ref="buiMeetingFacility">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="配置" prop="config">
            <template slot-scope="scope">
              <el-input v-model="scope.row.config" placeholder="请输入配置" />
            </template>
          </el-table-column>
          <el-table-column label="配置信息" prop="configInfo">
            <template slot-scope="scope">
              <el-input v-model="scope.row.configInfo" placeholder="请输入配置信息" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBuiMeeting, getBuiMeeting, delBuiMeeting, addBuiMeeting, updateBuiMeeting } from "@/api/park/buiMeeting";
import FileUpload from "../components/FileUpload.vue";
import { getListByLinkId } from "@/api/system/sysFile";

export default {
  name: "BuiMeeting",
  components: {
    FileUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      showFilePop: false,
      options: [{
        value: '会议中心',
        label: '会议中心'
      }, {
        value: '培训中心',
        label: '培训中心'
      }],
      lease_ways:[{
        value: '面议',
        label: '面议'
      }, {
        value: '半天/天',
        label: '半天/天'
      }],
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBuiMeetingFacility: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会务管理表格数据
      buiMeetingList: [],
      // 会议室配套表格数据
      buiMeetingFacilityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roomName: null,
        leaseWay: null,
        capacity: null,
        state: null,
        areaPrice: null,
        category: null,
      },
      // 表单参数
      form: {
        address: '重庆市江津区德感街道德园路409号',
        fileList: [],

      },
      fileList:[],
      // 表单校验
      rules: {
        category: [
          { required: true, message: "会议室类别不能为空", trigger: "blur" },
        ],
        roomName: [
          { required: true, message: "会议室名称不能为空", trigger: "blur" },
        ],
        leaseWay: [
          { required: true, message: "租赁方式不能为空", trigger: "blur" },
        ],
        capacity: [{ required: true, message: "容纳人数不能为空", trigger: "blur" }],
        builtUpArea: [
          { required: true, message: "面积不能为空", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会务管理列表 */
    getList() {
      this.loading = true;
      listBuiMeeting(this.queryParams).then(response => {
        this.buiMeetingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    filePopClose(){
      this.showFilePop=false
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        roomName: null,
        leaseWay: null,
        buildingId: null,
        floor: null,
        houseNumber: null,
        capacity: null,
        builtUpArea: null,
        state: null,
        areaPrice: null,
        category: null,
        feature: null,
        fee: null,
        address: '重庆市江津区德感街道德园路409号',
        fileList: [],
      };
      this.buiMeetingFacilityList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.fileList = [];
      this.title = "添加会务管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBuiMeeting(id).then(response => {
        this.form = response.data;
        this.buiMeetingFacilityList = response.data.buiMeetingFacilityList;
        this.open = true;
        this.title = "修改会务管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form));
          data.buiMeetingFacilityList = this.buiMeetingFacilityList;
          if (this.form.id != null) {
            updateBuiMeeting(data).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuiMeeting(data).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除会务管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBuiMeeting(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
	/** 会议室配套序号 */
    rowBuiMeetingFacilityIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 会议室配套添加按钮操作 */
    handleAddBuiMeetingFacility() {
      let obj = {};
      obj.config = "";
      obj.configInfo = "";
      this.buiMeetingFacilityList.push(obj);
    },
    /** 会议室配套删除按钮操作 */
    handleDeleteBuiMeetingFacility() {
      if (this.checkedBuiMeetingFacility.length == 0) {
        this.$alert("请先选择要删除的会议室配套数据", "提示", { confirmButtonText: "确定", });
      } else {
        this.buiMeetingFacilityList.splice(this.checkedBuiMeetingFacility[0].index - 1, 1);
      }
    },
    /** 单选框选中数据 */
    handleBuiMeetingFacilitySelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.buiMeetingFacility.clearSelection();
        this.$refs.buiMeetingFacility.toggleRowSelection(selection.pop());
      } else {
        this.checkedBuiMeetingFacility = selection;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('park/buiMeeting/export', {
        ...this.queryParams
      }, `park_buiMeeting.xlsx`)
    },
    showFileDowFn(row){
      this.fileList = [];
      const id = row.id ;
/*      this.fileList = row.fileList.forEach((element) => {
        element.url = "http://" + element.url;
      })*/
      getListByLinkId(id).then((response) => {
        response.data.forEach((element) => {
          let url= "http://" + element.url
          this.fileList.push({
            name: element.name,
            id: element.id,
            url,
          })
          //console.log(element);
        });
      });
      this.showFilePop=true
    },
  }
};
</script>
